package com.gjs.demo002.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gjs.demo002.R;
import com.gjs.demo002.bean.UserInfo;

import java.util.List;

public class MyListAdapter extends BaseAdapter {
    private Context context;
    private List<UserInfo> list;
    public MyListAdapter(Context context, List<UserInfo> data) {
        this.context = context;
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        if (view == null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.listview_item,viewGroup,false);
            viewHolder.nameTextView = view.findViewById(R.id.list_item_name);
            viewHolder.ageTextView = view.findViewById(R.id.list_item_age);
            viewHolder.sexTextView = view.findViewById(R.id.list_item_sex);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // 获取当前的userInfo
        UserInfo userInfo = list.get(i);

        // 给item设置值
        viewHolder.nameTextView.setText(userInfo.getUserName());
        viewHolder.ageTextView.setText(userInfo.getAge()+"");
        viewHolder.sexTextView.setText(userInfo.getSex());

        return view;
    }

    private final class ViewHolder{
        TextView nameTextView;
        TextView ageTextView;
        TextView sexTextView;

    }

}
